/* -+- file  ./kf2/GUI/AccountListDialog.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Mono.Unix;
using Habazie.kf2.Core;
using Habazie.kf2.Exposition;
using Habazie.kf2.Net;

namespace Habazie.kf2.GUI
{
	
	public class AccountListDialog : Gtk.Dialog
	{
		#region Fields
		private App m_App;
		private Gtk.ListStore m_Store;
		protected Gtk.TreeView m_Accounts;


		#endregion
		#region Constructors
		public AccountListDialog(App app) : base ()
		{
			m_App = app;
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.AccountListDialog));
			
			m_Store = new Gtk.ListStore (typeof (Account), typeof (string));
			m_Accounts.Model = m_Store;
			Gtk.CellRendererText text = new Gtk.CellRendererText ();
			Gtk.TreeViewColumn column = new Gtk.TreeViewColumn ("Account", text, "markup", 1);
			m_Accounts.AppendColumn (column);
			m_App.Connections.Changed += OnChanged;
//			m_App.Connections.AccountAdded += delegate (object o, AccountEventArgs args) {
//				Account acc = args.Account;
//				acc.Changed += 
//			}

			Update ();
		}
		#endregion
		#region Methods
		
		private void Update ()
		{
			m_Store.Clear ();
			if (m_App.Connections != null) {
				foreach (Account acc in m_App.Connections) {
					m_Store.AppendValues (acc, String.Format ("{0}\n<small><i>{1}@{2}</i></small>",
																acc.Name, acc.User, acc.Server));
				}
			}
		}
		
		private Account GetSelectedAccount ()
		{
			Gtk.TreeSelection sel = m_Accounts.Selection;
			Gtk.TreeModel model;
			Gtk.TreeIter iter;
			if (sel.GetSelected (out model, out iter)) {
				Account acc = (Account) model.GetValue (iter, 0);
				return acc;
			}
			return null;
		}

		#endregion
		#region Event handlers

		protected virtual void OnCloseClicked(object sender, System.EventArgs e)
		{
			Destroy ();
		}

		protected virtual void OnAddClicked(object sender, System.EventArgs e)
		{
			Account acc = new Account ();
			acc.Name = "New Account";
			acc.User = "";
			acc.Server = "";
			m_App.Connections.Add (acc);
			AccountDialog win = new AccountDialog (acc);
			win.Show ();
		}

		protected virtual void OnPropertiesClicked(object sender, System.EventArgs e)
		{
			Account acc = GetSelectedAccount ();
			if (acc != null) {
				AccountDialog win = new AccountDialog (acc);
				win.Show ();
			}
		}

		protected virtual void OnDeleteClicked(object sender, System.EventArgs e)
		{
			Account acc = GetSelectedAccount ();
			if (acc != null) {
				if (Reveal.Question (this, Catalog.GetString ("Do you wan to remove this account?")))
					m_App.Connections.Remove (acc);
			}
		}
		
		
		protected virtual void OnChanged (object sender, System.EventArgs args)
		{
			Update ();
		}
		#endregion
	}
	
}
